package com.qinghe.pro.controller.front;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qinghe.pro.entity.main.LogInfo;
import com.qinghe.pro.entity.main.Suggest;
import com.qinghe.pro.service.main.LogInfoService;
import com.qinghe.pro.service.main.SuggestService;
import com.qinghe.pro.shiro.ShiroUser;
import com.qinghe.utils.AjaxObject;
import com.qinghe.utils.Page;
import com.qinghe.utils.SecurityUtils;

@Controller
@RequestMapping("/main")
public class MainController {

	@Autowired
	@Qualifier("logInfoServiceImpl")
	private LogInfoService logInfoService;

	@Autowired
	@Qualifier("suggestServiceImpl")
	private SuggestService suggestService;

	private static final String UPLOAD = "/main/upload";
	private static final String DOWNLOAD = "/main/download";
	private static final String LOGINFO = "/main/log_info_list";
	private static final String SUGGEST = "/main/suggest";

	/**
	 * 进入上传页
	 */
	@RequiresUser
	@RequestMapping(value = "/upload", method = RequestMethod.GET)
	public String preUpload() {
		return UPLOAD;
	}

	/**
	 * 进入下载页
	 */
	@RequiresUser
	@RequestMapping(value = "/download", method = RequestMethod.GET)
	public String preDownload(Page page, Map<String, Object> map) {
		ShiroUser shiroUser = SecurityUtils.getShiroUser();
		LogInfo logInfo = new LogInfo();
		logInfo.setUsername(shiroUser.getLoginName());
		logInfo.setType("上传记录");
		List<LogInfo> logInfoList = logInfoService.findByPage(logInfo, page);

		map.put("page", page);
		map.put("logInfoList", logInfoList);

		return DOWNLOAD;
	}

	/**
	 * 我的记录
	 * 
	 * @param type "上传记录"/"下载记录"/"API记录"
	 * @param page
	 * @param map
	 */
	@RequiresUser
	@RequestMapping(value = "/logInfo/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String logInfoList(String type, Page page, Map<String, Object> map) {
		ShiroUser shiroUser = SecurityUtils.getShiroUser();
		LogInfo logInfo = new LogInfo();
		logInfo.setUsername(shiroUser.getLoginName());
		logInfo.setType(type);
		List<LogInfo> logInfoList = logInfoService.findByPage(logInfo, page);

		map.put("page", page);
		map.put("logInfoList", logInfoList);

		return LOGINFO;
	}

	/**
	 * 进入意见页
	 */
	@RequiresUser
	@RequestMapping(value = "/suggest", method = RequestMethod.GET)
	public String preSuggest() {
		return SUGGEST;
	}

	/**
	 * 发布意见
	 */
	@RequiresUser
	@RequestMapping(value = "/suggest", method = RequestMethod.POST)
	public @ResponseBody AjaxObject preSuggest(Suggest suggest) {
		suggestService.insert(suggest);

		return AjaxObject.newOk("意见发布成功！");
	}

}
